#include "sdk.h"
#include "config.h"

int strlen(char * xistr);
int strlen(char * xistr)
{
  int i = 0;
  while (*xistr)
  {
    i++;
  }
  return(i);
}

void _start(unsigned long, unsigned long *, const char*) __attribute__ ((section (".text.start")));
void _start(unsigned long arglen, unsigned long *argp, const char* binfile){
        // binary loader adapted from TN to work with pretty much every binary file
        int pathlen = strlen(MBCFW_ROOT);

		/* Read binfile to memory */
		SceUID fd = sceIoOpen(binfile, PSP_O_RDONLY, 0);
		sceIoRead(fd, (void *)0x08D20000, 0x10000);
		sceIoClose(fd);

		/* args: length of savedata path without "TV.BIN" */
		/* argp: the path */
		/* Example: call(28, "ms0:/PSP/SAVEDATA/NPEZ00176/TV.BIN"); */
		void (* call)(SceSize args, void *argp) = (void *)0x08D20000;
        sceKernelDcacheWritebackAll();
		call(pathlen, binfile);
}
